function timeout(time) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve();
      }, time);
    });
  }
  // timeout(1000)
  // timeout(1000)
  // timeout(1000)
  // ...
  class SuperTask {
    constructor(paralleCount = 2) {
      this.paralleCount = paralleCount; // 并发量
      this.tasks = [];
      this.runningCount = 0; // 正在运行的任务量
    }
    add(task) {
      return new Promise((resolve, reject) => {
        this.tasks.push({
          // 10
          task,
          resolve,
          reject,
        });
  if(this.runningCount < this.paralleCount){
    this._run();
  }
        
      });
    }
    _run() {
      // 依次运行tasks中的任务
      while (this.runningCount < this.paralleCount && this.tasks.length) {
        const { task, resolve, reject } = this.tasks.shift();
        this.runningCount++;
        task()
          .then(resolve, reject)
          .finally(() => {
            this.runningCount--;
            this._run();
          });
      }
    }
  }
  const superTask = new SuperTask();
  function addTask(time, name) {
    superTask
      .add(() => timeout(time))
      .then(() => {
        console.log(`任务${name}完成`);
      });
  }
  addTask(10000, 1);
  addTask(2000, 2);
  addTask(5000, 3);
  addTask(1000, 4);
  addTask(7000, 5);
  addTask(3000, 6);
  